package net.aydx84318.weather.bean;

import net.mihome.app.bean.net.GsonUtils;
import net.mihome.app.bean.net.ResponsePojo;

import org.apache.http.Header;

import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;

public abstract class WeatherAsyncHttpResponseHandler extends AsyncHttpResponseHandler {
	public final static Gson GSON=new Gson();
	@Override
	public void onFailure(int code, Header[] headers, byte[] arg2, Throwable arg3) {
		onFailure(code,headers,new Exception("服务器或网络异常，请稍后再试"));
	}

	@Override
	public void onSuccess(int code, Header[] headers, byte[] jsonResponse) {
		if(jsonResponse==null||jsonResponse.length<1){
			onFailure(code,headers,jsonResponse,new Exception("服务器或网络异常，请稍后再试"));
		}else{
			String jsonString=new String(jsonResponse);
			try{
				WeatherInfo weatherInf=GSON.fromJson(jsonString,WeatherInfo.class);
				onSuccess(code, headers, weatherInf);
			}catch(Exception e){
				onFailure(code,headers,new Exception(e.getMessage()));
			}
		}
	}
	public abstract void onSuccess(int arg0, Header[] arg1,WeatherInfo weatherInfo);
	public abstract void onFailure(int arg0, Header[] arg1, Throwable arg3);
}
